package com.absorbx.security;

import java.util.HashSet;
import java.util.Set;

import org.apache.jackrabbit.ocm.manager.collectionconverter.impl.BeanReferenceCollectionConverterImpl;
import org.apache.jackrabbit.ocm.mapper.impl.annotation.Collection;
import org.apache.jackrabbit.ocm.mapper.impl.annotation.Field;
import org.apache.jackrabbit.ocm.mapper.impl.annotation.Node;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.userdetails.UserDetails;

import flexjson.JSON;

/**
 * @author Ke
 * @version 1.0
 * @created 15-May-2009 11:59:22 AM
 */
@Node(jcrType = "ax:user")
public class User implements UserDetails {

	private static final long serialVersionUID = -5998882681253755931L;
	@Field(path = true)
	private String path;
	@Field(jcrName = "ax:email")
	private String email;
	@Field(jcrName = "ax:username")
	private String username;
	// Spring security UserDetails adapter
	@Field(jcrName = "ax:firstname")
	private String firstName;
	@Field(jcrName = "ax:lastname")
	private String lastName;
	@Field(jcrName = "ax:password")
	private String password;

	@Field(jcrName = "ax:enabled")
	private boolean enabled;
	@Field(jcrName = "ax:accountNonExpired")
	private boolean accountNonExpired;
	@Field(jcrName = "ax:accountNonLocked")
	private boolean accountNonLocked;
	@Field(jcrName = "ax:credentialsNonExpired")
	private boolean credentialsNonExpired;
	@Collection(collectionConverter = BeanReferenceCollectionConverterImpl.class, jcrName = "ax:roles")
	private Set<Role> roles = new HashSet<Role>();
	@Field(uuid = true)
	private String uuid;

	public String getUuid() {
		return uuid;
	}

	public void setUuid(String uuid) {
		this.uuid = uuid;
	}

	public User() {

	}

	public User(String username, String password) {
		this.username = username;
		this.password = password;
		this.enabled = true;
		this.accountNonExpired = true;
		this.accountNonLocked = true;
		this.credentialsNonExpired = true;
	}

	@Override
	public String getUsername() {
		return this.username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getPath() {
		return this.path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	public void setAccountNonExpired(boolean accountExpired) {
		this.accountNonExpired = accountExpired;
	}

	public void setAccountNonLocked(boolean accountLocked) {
		this.accountNonLocked = accountLocked;
	}

	public void setCredentialsNonExpired(boolean credentialsExpired) {
		this.credentialsNonExpired = credentialsExpired;
	}

	public void addRole(Role role) {
		roles.add(role);
	}

	public Set<Role> getRoles() {
		return roles;
	}

	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}

	@JSON(include = false)
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@JSON(include = false)
	public GrantedAuthority[] getAuthorities() {
		return roles.toArray(new GrantedAuthority[0]);
	}

	public boolean isEnabled() {
		return enabled;
	}

	public boolean isAccountNonExpired() {
		return accountNonExpired;
	}

	public boolean isAccountNonLocked() {
		return accountNonLocked;
	}

	public boolean isCredentialsNonExpired() {
		return credentialsNonExpired;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((email == null) ? 0 : email.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (email == null) {
			if (other.email != null)
				return false;
		} else if (!email.equals(other.email))
			return false;
		return true;
	}

}